package com.ruoyi.project.system.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.domain.*;
import com.ruoyi.project.system.domain.dto.*;
import com.ruoyi.project.system.mapper.*;
import com.ruoyi.project.system.service.ISysPhoneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/frontPhone")
public class DtFrontPhoneInfoController extends BaseController {

    @Autowired
    private ISysPhoneService isps;

    @Autowired
    private DtPhoneMapper dpm;

    @Autowired
    private DtQueryMapper dqlm;

    @Autowired
    private DtFinalResultMapper dfrm;

    @Autowired
    private DtRiskMapper drm;

    @GetMapping("/incomeHead")
    public AjaxResult incomeHead(@NotBlank Integer queryId) {
        DtQuery dql = dqlm.selectDtQueryByQueryId(queryId);
        AjaxResult result = new AjaxResult();
        result.put("risk", drm.queryRiskList(new DtRisk(dql)));
        result.put("flow", isps.flowState(dql.getQueryId()));

        String thrEle = dfrm.queryInfoResult(dql.getQueryId(), 17);

        if (StringUtils.isEmpty(thrEle)) {
            result.put("thrEleResult", "");
            result.put("belDuration", "");
        } else {
            JSONObject thrEleJs = JSONObject.parseObject(thrEle);  // MGZ TODO 2025/6/25: 待处理
            result.put("thrEleResult", 1);
            result.put("belDuration", "四川成都 12个月");
        }

        Map<String, String> info = dpm.incomeHeadBaseInfo(dql.getQueryId());
        if (info != null) {
            result.putAll(info);
        } else {
            result.put("device", "");
            result.put("location", "");
        }
        return result;
    }

    @GetMapping("/baseInfo")
    public AjaxResult baseInfo(@NotBlank Integer queryId) {
        Long userId = SecurityUtils.getUserId();
        DtFinalResult finalResultInfo = dfrm.queryResult(userId, queryId, 1);
        if (finalResultInfo == null) {
            return success("未付费");
        }
        DtBaseinfo info = dpm.baseInfo(queryId);
        List<Contacts> cons = null;
        if (info != null && StringUtils.isNotEmpty(info.getBaseInfo())) {
            String baseInfo = info.getBaseInfo();
            JSONArray lxr = JSON.parseObject(baseInfo).getJSONArray("lxr");

            List<String> lxfs = new ArrayList<>();
            if (StringUtils.isNotEmpty(lxr.getJSONObject(0).getString("lxfs"))) {
                lxfs.add(lxr.getJSONObject(0).getString("lxfs"));
            }
            if (StringUtils.isNotEmpty(lxr.getJSONObject(1).getString("lxfs"))) {
                lxfs.add(lxr.getJSONObject(1).getString("lxfs"));
            }
            if (StringUtils.isNotEmpty(lxr.getJSONObject(2).getString("lxfs"))) {
                lxfs.add(lxr.getJSONObject(2).getString("lxfs"));
            }
            if (!lxfs.isEmpty()) {
                List<CallLog> callLogs = dpm.baseInfoCallLogList(lxfs, queryId);
                List<CallLogResult> callLogResults = isps.disCallLog(callLogs, new CallLog(queryId));
                cons = isps.disContacts(callLogResults, lxr);
            }
        }
        info.setCons(cons);
        return success(info);
    }

    @GetMapping("/appList")
    public Object appList(@NotBlank AppAuth appAuth) {
        Integer queryId = appAuth.getQueryId();
        Long userId = dqlm.selectUserIdByQueryId(queryId);
        DtFinalResult finalResultInfo = dfrm.queryResult(userId, queryId,52);

        if (finalResultInfo == null) {
            return success("未付费");
        }
        // MGZ TODO 2025/6/23: 单号本人校验
//        Long userId = SecurityUtils.getUserId();
//        if (userId != 1l) {
//            SiyuQuery siyuQuery = siyuQueryMapper.selectSiyuQueryByQueryId(textMessage.getQueryId());
//            if (siyuQuery.getUserId() != userId.intValue()) {
//                return error();
//            }
//        }
        startPage();
        List<AppAuth> list = dpm.appList(appAuth);
        return getDataTable(list);
    }

    @GetMapping("/textMessageList")
    public Object textMessageList(@NotBlank TextMessage textMessage) {
        Integer queryId = textMessage.getQueryId();
        Long userId = dqlm.selectUserIdByQueryId(queryId);
        DtFinalResult finalResultInfo = dfrm.queryResult(userId, queryId,51);
        if (finalResultInfo == null) {
            return success("未付费");
        }
        startPage();
        List<TextMessage> list = dpm.textMessageList(textMessage);
        if (StringUtils.isNotEmpty(textMessage.getName())) {
            for (TextMessage ab : list) {
                if (!textMessage.getName().equals(ab.getName())) {
                    list.remove(ab);
                }
            }
        }
        return getDataTable(isps.disTextMessage(list, textMessage));
    }

    @GetMapping("/callLogList")
    public AjaxResult callLogList(@NotBlank CallLog callLog) {
        Integer queryId = callLog.getQueryId();
        Long userId = dqlm.selectUserIdByQueryId(queryId);
        DtFinalResult finalResultInfo = dfrm.queryResult(userId, queryId,51);
        if (finalResultInfo == null) {
            return success("未付费");
        }

        List<CallLog> list = dpm.callLogList(callLog);
        if (list == null || list.isEmpty()) {
            warn("暂无");
        }
        return success(isps.disCallLog(list, callLog));
    }

    @GetMapping("/addressBookList")
    public Object addressBookList(@NotBlank AddressBook addressBook) {
        Integer queryId = addressBook.getQueryId();
        Long userId = dqlm.selectUserIdByQueryId(queryId);
        DtFinalResult finalResultInfo = dfrm.queryResult(userId, queryId,50);

        if (finalResultInfo == null) {
            return success("未付费");
        }
        startPage();
        List<AddressBook> list = dpm.addressBookList(addressBook);
        for (AddressBook ad : list) {
            List<Phone> phones = JSONArray.parseArray(ad.getPhones_(), Phone.class);
            if (StringUtils.isNotEmpty(addressBook.getPhones_())) {
                for (int i = 0; i < phones.size(); i++) {
                    if (!phones.get(i).getValue().contains(addressBook.getPhones_())) {
                        phones.remove(i);
                    }
                }
            }
            ad.setPhones(phones);
            ad.setPhones_("");
        }
        return getDataTable(list);
    }

    @GetMapping("/getThreeInfo")
    public AjaxResult getThreeInfo(Integer queryId) {
        Map<String, String> threeInfo = dpm.incomeHeadBaseInfo(queryId);
        return success(threeInfo);
    }

}

